﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using NJL.IDAL;
using Royalsoft.DBUtility;//Please add references
namespace NJL.SQLServerDAL
{
    /// <summary>
    /// 数据访问类:NJL_REG_SUPPORT
    /// </summary>
    public partial class NJL_REG_SUPPORT : INJL_REG_SUPPORT
    {
        public NJL_REG_SUPPORT()
        { }
        #region  Method

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from NJL_REG_SUPPORT");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.VarChar,20)};
            parameters[0].Value = ID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(NJL.Model.NJL_REG_SUPPORT model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into NJL_REG_SUPPORT(");
            strSql.Append("ID,SUPPORT_OBJECT_TYPE,SUPPORT_OBJECT_ID,IP,CREATE_DATE_TIME,SUPPORT_TYPE_CODE)");
            strSql.Append(" values (");
            strSql.Append("@ID,@SUPPORT_OBJECT_TYPE,@SUPPORT_OBJECT_ID,@IP,@CREATE_DATE_TIME,@SUPPORT_TYPE_CODE)");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.VarChar,20),
					new SqlParameter("@SUPPORT_OBJECT_TYPE", SqlDbType.Int,4),
					new SqlParameter("@SUPPORT_OBJECT_ID", SqlDbType.VarChar,20),
					new SqlParameter("@IP", SqlDbType.VarChar,30),
					new SqlParameter("@CREATE_DATE_TIME", SqlDbType.DateTime),
					new SqlParameter("@SUPPORT_TYPE_CODE", SqlDbType.Int,4)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.SUPPORT_OBJECT_TYPE;
            parameters[2].Value = model.SUPPORT_OBJECT_ID;
            parameters[3].Value = model.IP;
            parameters[4].Value = model.CREATE_DATE_TIME;
            parameters[5].Value = model.SUPPORT_TYPE_CODE;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(NJL.Model.NJL_REG_SUPPORT model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update NJL_REG_SUPPORT set ");
            strSql.Append("SUPPORT_OBJECT_TYPE=@SUPPORT_OBJECT_TYPE,");
            strSql.Append("SUPPORT_OBJECT_ID=@SUPPORT_OBJECT_ID,");
            strSql.Append("IP=@IP,");
            strSql.Append("CREATE_DATE_TIME=@CREATE_DATE_TIME,");
            strSql.Append("SUPPORT_TYPE_CODE=@SUPPORT_TYPE_CODE");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@SUPPORT_OBJECT_TYPE", SqlDbType.Int,4),
					new SqlParameter("@SUPPORT_OBJECT_ID", SqlDbType.VarChar,20),
					new SqlParameter("@IP", SqlDbType.VarChar,30),
					new SqlParameter("@CREATE_DATE_TIME", SqlDbType.DateTime),
					new SqlParameter("@SUPPORT_TYPE_CODE", SqlDbType.Int,4),
					new SqlParameter("@ID", SqlDbType.VarChar,20)};
            parameters[0].Value = model.SUPPORT_OBJECT_TYPE;
            parameters[1].Value = model.SUPPORT_OBJECT_ID;
            parameters[2].Value = model.IP;
            parameters[3].Value = model.CREATE_DATE_TIME;
            parameters[4].Value = model.SUPPORT_TYPE_CODE;
            parameters[5].Value = model.ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from NJL_REG_SUPPORT ");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.VarChar,20)};
            parameters[0].Value = ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from NJL_REG_SUPPORT ");
            strSql.Append(" where ID in (" + IDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public NJL.Model.NJL_REG_SUPPORT GetModel(string ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ID,SUPPORT_OBJECT_TYPE,SUPPORT_OBJECT_ID,IP,CREATE_DATE_TIME,SUPPORT_TYPE_CODE from NJL_REG_SUPPORT ");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.VarChar,20)};
            parameters[0].Value = ID;

            NJL.Model.NJL_REG_SUPPORT model = new NJL.Model.NJL_REG_SUPPORT();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = ds.Tables[0].Rows[0]["ID"].ToString();
                }
                if (ds.Tables[0].Rows[0]["SUPPORT_OBJECT_TYPE"] != null && ds.Tables[0].Rows[0]["SUPPORT_OBJECT_TYPE"].ToString() != "")
                {
                    model.SUPPORT_OBJECT_TYPE = int.Parse(ds.Tables[0].Rows[0]["SUPPORT_OBJECT_TYPE"].ToString());
                }
                if (ds.Tables[0].Rows[0]["SUPPORT_OBJECT_ID"] != null && ds.Tables[0].Rows[0]["SUPPORT_OBJECT_ID"].ToString() != "")
                {
                    model.SUPPORT_OBJECT_ID = ds.Tables[0].Rows[0]["SUPPORT_OBJECT_ID"].ToString();
                }
                if (ds.Tables[0].Rows[0]["IP"] != null && ds.Tables[0].Rows[0]["IP"].ToString() != "")
                {
                    model.IP = ds.Tables[0].Rows[0]["IP"].ToString();
                }
                if (ds.Tables[0].Rows[0]["CREATE_DATE_TIME"] != null && ds.Tables[0].Rows[0]["CREATE_DATE_TIME"].ToString() != "")
                {
                    model.CREATE_DATE_TIME = DateTime.Parse(ds.Tables[0].Rows[0]["CREATE_DATE_TIME"].ToString());
                }
                if (ds.Tables[0].Rows[0]["SUPPORT_TYPE_CODE"] != null && ds.Tables[0].Rows[0]["SUPPORT_TYPE_CODE"].ToString() != "")
                {
                    model.SUPPORT_TYPE_CODE = int.Parse(ds.Tables[0].Rows[0]["SUPPORT_TYPE_CODE"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM NJL_REG_SUPPORT ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" * ");
            strSql.Append(" FROM NJL_REG_SUPPORT ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "NJL_REG_SUPPORT";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
        /// <summary>
        /// 地方特色想买不想买
        /// </summary>
        /// <param name="typeId"></param>
        /// <param name="Id"></param>
        /// <returns></returns>
        public object GetListCount(string typeId, string Id, string supportTypeId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(*) from NJL_REG_SUPPORT");
            strSql.Append(" where SUPPORT_OBJECT_TYPE=@SUPPORT_OBJECT_TYPE");
            strSql.Append(" and SUPPORT_OBJECT_ID=@SUPPORT_OBJECT_ID");
            strSql.Append(" and SUPPORT_TYPE_CODE=@SUPPORT_TYPE_CODE");
            SqlParameter[] parameters ={new SqlParameter("@SUPPORT_OBJECT_TYPE",SqlDbType.Int,4),
                                       new SqlParameter("@SUPPORT_OBJECT_ID",SqlDbType.VarChar,20),
                                       new SqlParameter("@SUPPORT_TYPE_CODE",SqlDbType.VarChar,20)
                                      };
            parameters[0].Value = typeId;
            parameters[1].Value = Id;
            parameters[2].Value = supportTypeId;
            return DbHelperSQL.GetSingle(strSql.ToString(), parameters);
        }

    }
}

